const app = getApp()
const connector = require('../../utils/connector.js')
// 1D901B
Page({

  /**
   * 页面的初始数据
   */
  data: {
    /** 
    * 页面配置 
    */
    basics: wx.getStorageSync("basics"),
    shopClass: [],//商品类
    shopping:[],//商品
    pages:1,//当前页数
    typeId:0,
    font:'',
    bar_Height: wx.getSystemInfoSync().statusBarHeight + 45,
    font:'',
   
  },
  //商品分类切换
  shopTopOne(e){
    let that = this
    let ind = e.currentTarget.dataset.index
    let shopClass = that.data.shopClass
    let typeId = e.currentTarget.dataset.id
    for (let i = 0; i < shopClass.length;i++){
      if (i == ind){
        shopClass[i].color = '#1D901B'
        shopClass[i].border = '#1D901B'
      }else{
        shopClass[i].color = '#333333'
        shopClass[i].border = '#fff'
      }
    }
    that.setData({
      shopClass: shopClass,
      typeId: typeId
    })
    goodsList(that, typeId, 1)
  },

  /*跳转商品详情*/
  shoppings: function (e) {
    let shoptwo, shopse;
    app.globalData.shoppingId = e.currentTarget.dataset.id
    // console.log(e.currentTarget.dataset.id)
    wx.navigateTo({
      url: '/pages/index/particulars/particulars',
    })
  },

  /**跳转购物车 */
  shopCart:function(e){
    let addCarts = this.data.addCarts;
    wx.setStorageSync('addCarts', addCarts)
    wx.navigateTo({
      url: '/pages/index/shopCart/shopCart',
    })
  },

  /**
   * 生命周期函数--监听页面加载 substring
   */
  onLoad: function (options) {
    var that = this
    // console.log(options, "index814")
    const scene = decodeURIComponent(options.scene)
 
    // if (wx.getStorageSync('workuser') == ''){
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let that = this;
    

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    goodsCategory(that)

    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
   
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
   
  },

  /**
    * 页面相关事件处理函数--监听用户下拉动作
    */
   onPullDownRefresh: function () {
    var that = this
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this
    var pages = that.data.pages
    var typeId = that.data.typeId
    that.setData({
      pages: pages+1
    })
    goodsList(that, typeId, 1)

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var that = this
    let val = app.searchWord
    // console.log(val)
    return {
      title: "欢迎进入题库",
      // path: '/pages/card/card?id=share_' + wx.getStorageSync('USERID'),
      path: '/pages/first/first?goTo=2&school=' +  wx.getStorageSync('userInfo').school_id + '&pid='  + app.globalData.myId,
      success: function (res) {
      },
      fail: function (res) {
        // 分享失败
      }
    }
 
  }
})
//获取商品分类
function goodsCategory(that){
  connector.getFunc(connector.connector().goodsCategory, "", "get", app.tokens, function (res) {
    if (res.statusCode == 200) {
      let arr = res.data
      goodsList(that, arr[0].id, 1)
      for (let i = 0; i < arr.length; i++) {
        if (i == 0) {
          arr[i].color = "#1D901B"
          arr[i].border = "#1D901B"
        } else {
          arr[i].color = "#333333"
          arr[i].border = "#fff"
        }
      }
      that.setData({
        shopClass: arr,
        pages: 1,
        typeId: arr[0].id
      })
    } else {
      wx.showToast({
        title: res.statusCode + ":" + res.errMsg,
        icon: 'none',
        duration: 1000
      })
    }
  }, function (res) {
    wx.showToast({
      title: "内部错误",
      icon: 'none',
      duration: 1000
    })
  })
}
//获取商品
function goodsList(that,goodsid,page){
  // const pages = {
  //   page: page,
  //   status: statuss
  // }
  let shopping = that.data.shopping
  let font = ""
  that.setData({
    font: "正在加载"
  })
  connector.getFunc(connector.connector().goodsList + goodsid + "/" + page, "", "get", app.tokens, function (res) {
    if (res.statusCode == 200) {
      // console.log(res.data.data)
      if (page == 1) {
        shopping = res.data.data
        if (res.data.data.length < 1) {
          font = "暂无商品"
        } else {
          if (res.data.last_page == res.data.current_page) {
            font = "没有更多了"
          } else {
            font = "加载完成"
          }
        }
      } else {
        console.log("page>1")
        if (res.data.current_page <= res.data.last_page) {
          shopping = shopping.concat(res.data.data)
          if (res.data.current_page == res.data.last_page) {
            font = "没有更多了"
          } else {
            font = "加载完成"
          }
        }
      }
      // console.log(shopping)
      that.setData({
        shopping: shopping,
        font: font,
      })

    } else {
      wx.showToast({
        title: res.statusCode + ":" + res.errMsg,
        icon: 'none',
        duration: 1000
      })
    }
  }, function (res) {
    wx.showToast({
      title: "内部错误",
      icon: 'none',
      duration: 1000
    })
  })
}
